package com.example.hospital.controller;

import com.example.hospital.dto.PrescriptionQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Prescription;
import com.example.hospital.service.PrescriptionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("prescription")
@Tag(name = "Prescription", description = "处方管理相关接口")
public class PrescriptionController {
    @Resource
    private PrescriptionService prescriptionService;

    /**
     * 新增处方
     *
     * @param prescription
     * @return prescription id or error
     */
    @Operation(
            summary = "新增处方",
            description = "创建一个新的处方记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回处方ID"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result<Integer> addPrescription(@RequestBody Prescription prescription) {
        boolean saved = prescriptionService.save(prescription);
        return saved ? Result.success("添加成功", prescription.getPrescriptionId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除处方
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除处方",
            description = "通过处方ID删除指定的处方记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "处方记录未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result deletePrescription(@PathVariable("id") Integer id) {
        boolean removed = prescriptionService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改处方信息
     *
     * @param prescription
     * @return
     */
    @Operation(
            summary = "修改处方信息",
            description = "通过传递新的处方信息更新现有处方",
            responses = {
                    @ApiResponse(responseCode = "200", description = "修改成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updatePrescription(@RequestBody Prescription prescription) {
        boolean updated = prescriptionService.updateById(prescription);
        return updated ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id查询处方
     *
     * @param id
     * @return prescription or error
     */
    @Operation(
            summary = "根据id查询处方",
            description = "根据处方ID查询处方信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "处方记录未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Prescription> getPrescription(@PathVariable("id") Integer id) {
        Prescription prescription = prescriptionService.getById(id);
        return prescription != null ? Result.success("查询成功", prescription) : Result.fail("查询失败");
    }

    /**
     * 查询所有处方
     *
     * @return prescription list
     */
    @Operation(
            summary = "查询所有处方",
            description = "获取所有处方记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @GetMapping
    public Result listPrescriptions() {
        return Result.success(prescriptionService.list());
    }

    /**
     * 根据患者id查询处方
     *
     * @param patientId
     * @return prescription list
     */
    @Operation(
            summary = "根据患者id查询处方",
            description = "通过患者ID查询处方信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "患者未找到")
            }
    )
    @GetMapping("/patient/{patientId}")
    public Result<List<Prescription>> listPrescriptionsByPatientId(@PathVariable("patientId") Integer patientId) {
        List<Prescription> prescriptions = prescriptionService.lambdaQuery().eq(Prescription::getPatientId, patientId).list();
        return prescriptions != null ? Result.success("查询成功", prescriptions) : Result.fail("查询失败");
    }

    /**
     * 条件查询处方
     * @param prescriptionQuery
     * @return prescription list
     */
    @Operation(
            summary = "条件查询处方",
            description = "根据条件查询处方信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping("/query")
    public Result<List<Prescription>> queryPrescriptions(@RequestBody PrescriptionQuery prescriptionQuery) {
        List<Prescription> prescriptions = prescriptionService.listPrescriptions(prescriptionQuery);
        return prescriptions != null ? Result.success("查询成功", prescriptions) : Result.fail("查询失败");
    }
}
